package cn.zhyjohn.paysystem.biz;
import cn.zhyjohn.paysystem.common.CreateQRCode;
import cn.zhyjohn.paysystem.common.Response;
import cn.zhyjohn.paysystem.requset.WxRefundParams;
import cn.zhyjohn.paysystem.service.WeChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author JOHN
 * @title: WeChatPayManager
 * @projectName paysystem
 * @since 2019/8/28
 */
@Component
public class WeChatPayManager {

    @Autowired
    private WeChatService chatService;

    /**
     * 微信通知回调
     * @author JOHN
     * @date 16:43
     **/
    public String payNotify(HttpServletRequest request) {
        return chatService.payNotify(request);
    }

    /**
     * 微信H5支付 <br>
     * 必须再web页面中发起支付且域名已添加到开发配置中
     * @author JOHN
     * @date 14:55
     * @param total_fee 金额
     **/
    public Response wapPay(HttpServletRequest request, String total_fee) {
        return chatService.wapPay(request, total_fee, "测试");
    }

    /**
     *  微信公众号支付
     * @author JOHN
     * @date 14:57
     * @param openId 微信用户id
     * @param total_fee 金额
     **/
    public Response webPay(HttpServletRequest request, String openId, String total_fee) {
        return chatService.webPay(request, openId, total_fee, "测试");
    }

    /**
     * 微信生成支付二维码（模式一）并在页面上显示
     * @author JOHN
     * @date 14:58
     * @param product_id 商品id
     **/
    public void scanCodeOne(HttpServletResponse response, String product_id) throws IOException {
        Response resp = chatService.scanCodeOne(product_id);
        CreateQRCode.encoderQRCode(resp.getData().toString(),response.getOutputStream());
    }

    /**
     * 微信扫码支付模式二
     * @author JOHN
     * @date 9:50
     * @param orderNo 订单号
     * @param total_fee 金额
     * @return void
     **/
    public Response scanCodeTwo(HttpServletRequest request, HttpServletResponse response, String orderNo, String total_fee) throws IOException {
        Response resp = chatService.scanCodeTwo(request, orderNo, total_fee);
        if (resp.isSuccess()) {
            response.setContentType("image/png");
            response.setHeader("Cache-Control", "no-store, no-cache");
            CreateQRCode.encoderQRCode(resp.getData().toString(),response.getOutputStream());
            return null;
        }
        return resp;
    }

    /**
     * 微信APP支付
     *
     * @param body      描述
     * @param orderNo   订单号
     * @param total_fee 金额
     * @author JOHN
     * @date 15:31
     **/
    public Response appPay(HttpServletRequest request, String body, String orderNo, String total_fee) {
        return chatService.appPay(request, body, orderNo, total_fee);
    }

    /**
     * 微信小程序支付
     *
     * @param openid    微信用户id
     * @param body      描述
     * @param orderNo   订单号
     * @param total_fee 金额
     * @author JOHN
     * @date 15:34
     **/
    public Response jsapi(HttpServletRequest request, String openid, String body, String orderNo, String total_fee) {
        return chatService.jsapi(request, openid, body, orderNo, total_fee);
    }

    /**
     * 微信提现
     *
     * @param openId 微信用户id
     * @param body 描述
     * @param amount 金额
     * @author JOHN
     * @date 10:50
     **/
    public Response transfers(HttpServletRequest request, String openId, String body, String amount) {
        return chatService.transfers(request, openId, body, amount);
    }

    /**
     * 微信退款
     * @author JOHN
     * @date 15:00
     **/
    public Response refund(WxRefundParams params) {
        return chatService.refund(params);
    }
}
